<template>
    <div class="table">
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item :to="{ path: '/home' }"><i class="el-icon-menu"></i> 系统管理</el-breadcrumb-item>
                <el-breadcrumb-item>部门管理</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="handle-box">
            <el-button type="primary" icon="delete" class="handle-del mr10" @click="add">新增</el-button>
        </div>
        <el-table :data="deptList" border style="width: 100%" ref="multipleTable"
                  @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="45"></el-table-column>
            <el-table-column prop="id" label="ID" sortable>
            </el-table-column>
            <el-table-column prop="fullname" label="部门全称">
            </el-table-column>
            <el-table-column prop="simplename" label="部门简称">
            </el-table-column>
            <el-table-column prop="num" label="排序">
            </el-table-column>
            <el-table-column prop="tips" label="备注">
            </el-table-column>
            <el-table-column label="操作">
                <template scope="scope">
                    <el-button size="mini" type="primary"
                               @click="handleEdit(scope.$index, scope.row)">编辑
                    </el-button>
                    <el-button size="mini" type="primary"
                               @click="handleDelete(scope.$index, scope.row)">删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <div class="pagination">
            <el-pagination
                @current-change="getData"
                :page-size="size"
                layout="total, prev, pager, next"
                :total="total">
            </el-pagination>
        </div>

        <div id="dialog">
            <!--新增弹框-->
            <el-dialog title="新增" :visible.sync="addFormVisible">
                <el-form :model="addForm" :inline="true" :rules="rules" ref="addForm" label-position="right"
                         class="demo-form-inline">
                    <el-form-item prop="simplename" label="部门简称：" :label-width="formLabeWidth">
                        <el-input v-model="addForm.simplename" auto-complete="off"></el-input>
                    </el-form-item>
                    <el-form-item prop="fullname" label="部门全称：" :label-width="formLabeWidth">
                        <el-input v-model="addForm.fullname" auto-complete="off"></el-input>
                    </el-form-item>
                    <el-form-item prop="num" label="排序：" :label-width="formLabeWidth">
                        <el-input v-model.number="addForm.num" auto-complete="off"></el-input>
                    </el-form-item>
                    <el-form-item prop="pid" label="上级部门：" :label-width="formLabeWidth">
                        <el-input v-model.number="addForm.pid" @focus="deptTreeFormVisible=true;"></el-input>
                    </el-form-item>
                    <el-form-item prop="tips" label="备注：" :label-width="formLabeWidth">
                        <el-input v-model.number="addForm.tips" auto-complete="off"></el-input>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="addFormVisible = false">取 消</el-button>
                    <el-button type="primary" @click="addSubmitForm('addForm')">确 定</el-button>
                </div>
            </el-dialog>
        </div>
        <div>
            <!--编辑弹框-->
            <el-dialog title="编辑" :visible.sync="editFormVisible">
                <el-form :model="editForm" :inline="true" :rules="rules" ref="editForm" label-position="right"
                         class="demo-form-inline">
                    <el-form-item prop="simplename" label="部门简称：" :label-width="formLabeWidth">
                        <el-input v-model="editForm.simplename" auto-complete="off"></el-input>
                    </el-form-item>
                    <el-form-item prop="fullname" label="部门全称：" :label-width="formLabeWidth">
                        <el-input v-model="editForm.fullname" auto-complete="off"></el-input>
                    </el-form-item>
                    <el-form-item prop="num" label="排序：" :label-width="formLabeWidth">
                        <el-input v-model.number="editForm.num" auto-complete="off"></el-input>
                    </el-form-item>
                    <el-form-item prop="pid" label="上级部门：" :label-width="formLabeWidth">
                        <el-input v-model.number="editForm.pid" @focus="deptTreeFormVisible=true;"></el-input>
                    </el-form-item>
                    <el-form-item prop="tips" label="备注：" :label-width="formLabeWidth">
                        <el-input v-model.number="editForm.tips" auto-complete="off"></el-input>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="editFormVisible = false">取 消</el-button>
                    <el-button type="primary" @click="editSubmitForm('editForm')">确 定</el-button>
                </div>
            </el-dialog>
        </div>
        <!--部门树-->
        <el-dialog title="选择部门" :visible.sync="deptTreeFormVisible">
            <el-tree
                show-checkbox
                check-strictly
                node-key="id"
                ref="deptTree"
                highlight-current
                lazy
                :expand-on-click-node="false"
                :load="loadNode"
                :props="defaultProps">
            </el-tree>
            <div slot="footer" class="dialog-footer">
                <el-button @click="deptTreeFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="selectTree">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    export default {
        data() {
            return {
                deptList: [],
                addFormVisible: false,
                editFormVisible: false,
                deptTreeFormVisible: false,
                addForm: {
                    simplename: "",
                    fullname: "",
                    num: "",
                    pid: "",
                    tips: ""
                },
                editForm: {
                    simplename: "",
                    fullname: "",
                    num: "",
                    pid: "",
                    tips: ""
                },
                formLabeWidth: '100px',
                rules: {
                    simplename: [
                        {required: true, message: '请输入部门简称', trigger: 'change'},
                    ],
                    fullname: [
                        {required: true, message: '请输入部门全称', trigger: 'change'}
                    ],
                    num: [
                        {type: 'number', required: true, message: '请输入排序', trigger: 'change'}
                    ],
                    pid: [
                        {type: 'number', required: true, message: '请选择上级部门', trigger: 'change'}
                    ]
                },
                defaultProps: {
                    children: 'children',
                    label: 'fullname'
                },
                total: 0,
                currentPage: 1,
                size: 10
            }
        },
        created() {
            this.getData();
        },
        methods: {
            //获取user数据
            getData(data) {
                let self = this;
                var Vstart = 0;
                var Vsize = self.size;
                if (data != undefined && !isNaN(data)) {
                    Vstart = data - 1;
                }
                self.$axios.post("/api/dept/get", {pageNumber: Vstart, pageSize: self.size}).then((res) => {
                    self.deptList = res.data.data.content;
                    self.total = res.data.data.totalElements;
                })
            },
            //新增面板
            add: function () {
                this.addFormVisible = true;
            },
            //添加菜单
            addSubmitForm(formName) {
                const self = this;
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        self.$axios.post('/api/dept/add', self.addForm)
                            .then(function (response) {
                                console.log(response);
                                self.$message({
                                    message: "添加成功",
                                    type: 'warning'
                                });
                                self.addFormVisible = false;
                                self.getData();
                            })
                            .catch(function (error) {
                                if (error.response) {
                                    self.$message({
                                        message: error.response.data.msg,
                                        type: 'error'
                                    });
                                }
                            })
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            //编辑面板
            handleEdit(index, row) {
                const self = this;
                console.log(row);
                //self.$message('编辑第'+(index+1)+'行');
                self.editForm = row;
                self.editFormVisible = true;
            },
            //修改菜单
            editSubmitForm(formName) {
                const self = this;
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        self.$axios.put('/api/dept/update', self.editForm)
                            .then(function (response) {
                                console.log(response);
                                self.$message({
                                    message: "修改成功",
                                    type: 'warning'
                                });
                                self.editFormVisible = false;
                                self.getData();
                            })
                            .catch(function (error) {
                                if (error.response) {
                                    self.$message({
                                        message: error.response.data.msg,
                                        type: 'error'
                                    });
                                }
                            })
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            //删除菜单
            handleDelete(index, row) {
                const self = this;
                console.log(row.id);
                self.$confirm('确认删除吗？', '提示', {
                    type: 'warning'
                }).then(function () {
                    self.$axios.delete('/api/dept/delete/' + row.id)
                        .then(function (response) {
                            console.log(response);
                            self.$message({
                                message: "删除成功",
                                type: 'warning'
                            });
                            self.getData();
                        })
                        .catch(function (error) {
                            if (error.response) {
                                self.$message({
                                    message: error.response.data.msg,
                                    type: 'error'
                                });
                            }
                        })
                })
            },
            handleSelectionChange(val) {
                this.multipleSelection = val;
            },
            //树形部门加载
            loadNode: function (node, addNode) {
                const self = this;
                var pid;
                if (node.id == 0) {
                    pid = 0;
                } else {
                    pid = node.data.id;
                }
                this.$axios.get('/api/dept/selectDeptTreeList/' + pid)
                    .then(function (data) {
                        addNode(data.data.data);
                    })
                    .catch(function (error) {
                        if (error.response) {
                            self.$message({
                                message: error.response.data.msg,
                                type: 'error'
                            });
                        }
                    })
            },
            //选择部门
            selectTree: function () {
                const self = this;
                var depts = self.$refs.deptTree.getCheckedNodes();
                if (depts.length == 0) {
                    self.$message.error('请选择一项！');
                    return;
                }
                if (depts.length > 1) {
                    self.$message.error('只能选择一项！');
                    return;
                }
                console.log(depts);
                self.addForm.pid = depts[0].id;
                self.editForm.pid = depts[0].id;
                self.deptTreeFormVisible = false;
            }
        }
    }
</script>

<style scoped>
    .handle-box {
        margin-bottom: 20px;
    }

    .handle-select {
        width: 120px;
    }

    .handle-input {
        width: 300px;
        display: inline-block;
    }
</style>
